import pygame
from abc import ABC, abstractmethod
from typing import Dict, Any, Optional


class BaseGame(ABC):
    """Base class for all mini-games in the framework"""
    
    def __init__(self, name: str, screen: pygame.Surface, resource_manager):
        self.name = name
        self.screen = screen
        self.resource_manager = resource_manager
        self.running = False
        self.clock = pygame.time.Clock()
        self.fps = 60
        
    @abstractmethod
    def initialize(self) -> None:
        """Initialize game-specific resources and settings"""
        pass
        
    @abstractmethod
    def handle_event(self, event: pygame.event.Event) -> None:
        """Handle game-specific events"""
        pass
        
    @abstractmethod
    def update(self, dt: float) -> None:
        """Update game logic"""
        pass
        
    @abstractmethod
    def render(self) -> None:
        """Render the game"""
        pass
        
    def run(self) -> None:
        """Main game loop"""
        self.initialize()
        self.running = True
        
        while self.running:
            dt = self.clock.tick(self.fps) / 1000.0
            
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.running = False
                    else:
                        self.handle_event(event)
                else:
                    self.handle_event(event)
                    
            self.update(dt)
            self.render()
            pygame.display.flip()
            
    def stop(self) -> None:
        """Stop the game"""
        self.running = False